/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '<a class="btn btn-success edit-program" href="/manager/program/create.html">创建</a>';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-state"><option value="">项目状态</option><option value="1">未开放</option><option value="2">申请中</option><option value="3">选房中</option><option value="4">已完成</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/program/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-state');

    $('#delete-program-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var title = button.data('title');
        $(this).find('.program-id').val(id);
        $(this).find('.program-title').text(title);
    });
    $('#delete-program-dialog').on('hide.bs.modal', function(){
        $(this).find('.program-id').val('');
        $(this).find('.program-title').text('');
    });

    $('#delete-program-dialog .btn-save').on('click', function(){
        var id = $('#delete-program-dialog').find('.program-id').val();
        sendAjax('/v1/manager/delete/program', 'post', {id:id}, function(json){
            $('#delete-program-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#program-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.state = $('.select-state').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'title',
            title: '项目名称'
        }, {
            field: 'state',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '未开放';
                    case 1 : return '申请中';
                    case 2 : return '选房中';
                    case 3 : return '已完成';
                    default : return '未开放';
                }
            }
        }, {
            field: 'effective_time',
            title: '有效时间'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-success edit-program"  href="/manager/program/edit/'+row.id+'.html">编辑</a>';
                if (row.state == 0) {
                    action += '<a class="btn btn-xs btn-info view-program" href="/manager/program/view/'+row.id+'.html">添加房源</a>';
                    action += '<a class="btn btn-xs btn-primary program-apart" href="/manager/program/'+row.id+'/apart.html">项目房源</a>';
                } else {
                    action += '<a class="btn btn-xs btn-primary program-apart" href="/manager/program/'+row.id+'/apart.html">项目房源</a>';
                    action += '<a class="btn btn-xs btn-primary program-user" href="/manager/program/'+row.id+'/user.html">申请记录</a>';
                    action += '<a class="btn btn-xs btn-primary  user-apart" href="/manager/program/user/'+row.id+'/apart.html">选房记录</a>';
                }
                action += '<a class="btn btn-xs btn-danger delete-program" data-toggle="modal" data-target="#delete-program-dialog" data-id="'+row.id+'" data-title="'+row.title+'">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('#program-table').bootstrapTable('refresh');
}